//
// DotNetNuke - http://www.dotnetnuke.com
// Copyright (c) 2002-2010
// by Perpetual Motion Interactive Systems Inc. ( http://www.perpetualmotion.ca )
//
// Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated 
// documentation files (the "Software"), to deal in the Software without restriction, including without limitation 
// the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and 
// to permit persons to whom the Software is furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all copies or substantial portions 
// of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED 
// TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL 
// THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF 
// CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 
// DEALINGS IN THE SOFTWARE.
//

using System;
using System.IO;
using System.Net;
using System.Web.UI.WebControls;
using System.Xml;
using DotNetNuke.Services.Localization;
using System.Xml.XPath;
namespace DotNetNuke.Modules.Admin.Marketplace
{
	partial class Marketplace : DotNetNuke.Entities.Modules.PortalModuleBase
	{
		private const string XSL_PATH = "RSS91.xsl";
		private const string PRODUCT_XMLNODE_PATH = "//channel/item";
		protected override void OnLoad(EventArgs e)
		{
			base.OnLoad(e);

			cmdGo.Click += new EventHandler(cmdGo_Click);

			try {
				if (!Page.IsPostBack) {
					PopulateControls();
				}
			} catch (Exception exc) {
				Services.Exceptions.Exceptions.ProcessModuleLoadException(this, exc);
			}
		}
		private void PopulateControls()
		{
			ListItem oListItem;
			XmlDocument oXMLDocument;
			XmlNodeList oNodes;
			try {
				oXMLDocument = new XmlDocument();
				oXMLDocument.Load(AppDomain.CurrentDomain.SetupInformation.ApplicationBase + "DesktopModules\\Admin\\Marketplace\\ListItems.xml");
				oNodes = oXMLDocument.SelectNodes("/listitems/feedtypes/feedtype");
				foreach (XmlNode oNode in oNodes) {
					oListItem = new ListItem();
					oListItem.Value = oNode.ChildNodes.Item(0).InnerText;
					oListItem.Text = oNode.ChildNodes.Item(1).InnerText;
					cboSearchFor.Items.Add(oListItem);
				}
				oNodes = oXMLDocument.SelectNodes("/listitems/categories/category");
				foreach (XmlNode oNode in oNodes) {
					oListItem = new ListItem();
					oListItem.Value = oNode.ChildNodes.Item(0).InnerText;
					oListItem.Text = oNode.ChildNodes.Item(1).InnerText;
					cboCategories.Items.Add(oListItem);
				}
			} catch (Exception oExc) {
				throw oExc;
			}
		}
		protected void cmdGo_Click(object sender, System.EventArgs e)
		{
			HttpWebRequest oRequest;
			WebResponse oResponse;
			Stream oStream;
			XmlTextReader oReader;
			XPathDocument oXMLDocument;
			string sFeedType;
			string sCategoryID;
			string sRequest;
			try {
				sFeedType = cboSearchFor.SelectedValue;
				sCategoryID = cboCategories.SelectedValue;
				sRequest = "http://www.snowcovered.com/Snowcovered2/DesktopModules/PortalStore/rss.aspx?feedtype=" + sFeedType + "&categoryid=" + sCategoryID;
				try {
					oRequest = (HttpWebRequest)WebRequest.Create(sRequest);
					oRequest.Timeout = 10000;
					oResponse = oRequest.GetResponse();
					oStream = oResponse.GetResponseStream();
				} catch (System.Exception oExc) {
					throw oExc;
				}
				oReader = new XmlTextReader(oStream);
				oReader.XmlResolver = null;
				oXMLDocument = new XPathDocument(oReader);
				XPathNavigator nav = oXMLDocument.CreateNavigator();
				xmlRSS.XPathNavigator = nav;
				//xmlRSS.Document = oXMLDocument;
				xmlRSS.TransformSource = XSL_PATH;
				if (nav.Select(PRODUCT_XMLNODE_PATH).Count == 0) {
					lblMessage.Text = Localization.GetString("NoProductsFound.Text", this.LocalResourceFile);
				}
			} catch (Exception oExc) {
				throw oExc;
			}
		}
	}
}
